package com.example.demo.service;

import cn.felord.api.WorkWeChatApi;
import cn.felord.domain.WeComResponse;
import cn.felord.domain.webhook.WebhookBody;
import cn.felord.domain.webhook.WebhookMarkdownBody;
import cn.felord.domain.webhook.WebhookTemplateCardBody;
import cn.felord.domain.webhook.WebhookTextBody;
import cn.felord.domain.webhook.card.*;
import com.example.demo.dto.OAformDto;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class WeChatBoTService {

    public void sendMessage_MD(String key, OAformDto dto) {
        WebhookBody body = WebhookMarkdownBody.from("## <font color=\"warning\">Ard程序挂起</font>\n" +
                ">程序名：`" + dto.getCnccxmc() + "`\n" +
                ">挂起人：<font color=\"comment\">" + dto.getOaId() + "</font>\n" +
                ">原因：<font color=\"comment\">" + dto.getGqyy() + "</font>\n" +
                ">挂起时间：<font color=\"info\">" + dto.getGqrq() + "</font>\n" +
                "><@张小飞>");
        WorkWeChatApi.webhookApi().send(key, body).isSuccessful();
    }

    public void sendMessage_text(String key) {
        List<String> atMobiles = new ArrayList<>();
        atMobiles.add("15029443408");
        WebhookBody body = WebhookTextBody.from("这里为纯文本", null, atMobiles);
        WeComResponse weComResponse = WorkWeChatApi.webhookApi().send(key, body);
        weComResponse.isSuccessful();
    }

    public void sendMessage_Card(String key, OAformDto dto) {
        MainTitle mainTitle = new MainTitle("木工CNC", "Ard程序挂起提醒");
        UrlCardAction cardAction = new UrlCardAction("http://10.10.10.104:8000/pages/cncindex.html");
//        EmphasisContent emphasisContent = new EmphasisContent();
//        emphasisContent.setTitle("197");
//        emphasisContent.setDesc("测试");
        CardSource source = new CardSource();
        source.setIconUrl("http://www-x-tonwel-x-com.img.addlink.cn/favicon.jpg");
        source.setDesc("生产中心程序管理");
        source.setDescColor(CardSource.DescColor.GREEN);
        List<HorizontalContent> horList = new ArrayList<>();
        TextHorizontalContent text1 = new TextHorizontalContent("程序名", dto.getCnccxmc());
        TextHorizontalContent text2 = new TextHorizontalContent("挂起人", dto.getOaId());
        TextHorizontalContent text3 = new TextHorizontalContent("原因", dto.getGqyy());
        TextHorizontalContent text4 = new TextHorizontalContent("挂起时间", dto.getGqrq());
        horList.add(text1);
        horList.add(text2);
        horList.add(text3);
        horList.add(text4);
        TextTemplateCard textTemplateCard = new TextTemplateCard(mainTitle, cardAction)
                .source(source)
//                .emphasisContent(emphasisContent)
                .horizontalContentList(horList)
                .jumpList(Collections.singletonList(new UrlJump("@张小飞", "http://10.10.10.104:8000/pages/cncindex.html")));
        WebhookTemplateCardBody textCardBody = WebhookTemplateCardBody.from(textTemplateCard);

        WorkWeChatApi.webhookApi().send(key, textCardBody).isSuccessful();
    }
}
